package com.liuya.safe.policy.handler;

import com.liuya.base.BaseModel;
import com.liuya.common.WebUtil;
import com.liuya.safe.model.SafeUserCategory;
import com.liuya.safe.tree.AbstractTreeHandler;
import com.liuya.safe.util.Factory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;

/**
 * 用户类别处理
 */
@Component("userCategoryTreeHandler")
public class UserCategoryTreeHandler extends AbstractTreeHandler implements Serializable {

//    @Autowired
//    @Qualifier("userCategoryManagerService")
//    private UserCategoryManagerService manager;

    @Override
    public String getPageTitle() {
        return "User_Category_Tree";
    }

    @Override
    public String getCreatePage() {
        return "policy/userCategory/create";
    }

    @Override
    public String getNodeName(BaseModel node) {
        SafeUserCategory uc = (SafeUserCategory) node;
        return uc.getName();
    }

    @Override
    public Collection getAllNodes() {
//        return manager.getAllUserCategories();
        return Factory.getUserCategoryManager("").getAllUserCategories();
    }

    @Override
    public BaseModel getNode(HttpServletRequest req) {
        SafeUserCategory uc = new SafeUserCategory();
        String id = getId(req);
        String pid = getParentId(req);
        String name = req.getParameter("name");
        String desc = req.getParameter("description");
        boolean isLeaf = WebUtil.getBooleanParameter(req, "isLeaf", false);

        uc.setId(id);
        uc.setIsLeaf(isLeaf);
        uc.setName(name);
        uc.setPid(pid);
        uc.setDescription(desc);
        return uc;
    }

    @Override
    public BaseModel getNodeByName(String name) {
        Collection nodes = Factory.getUserCategoryManager("").getAllUserCategories();
        for (Iterator iter = nodes.iterator(); iter.hasNext();) {
            SafeUserCategory node = (SafeUserCategory) iter.next();
            if (node.getName().equals(name)) {
                return node;
            }
        }
        return null;
    }

    @Override
    public boolean addNode(Object node) {
        SafeUserCategory uc = (SafeUserCategory) node;
        try{
            Factory.getUserCategoryManager("").addUserCategory(uc.getPid(), uc.getName(), uc.getDescription(), uc.getIsLeaf());
        } catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean deleteNode(String id) {
        try{
            Factory.getUserCategoryManager("").deleteUserCategory(id);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean updateNode(Object node) {
        SafeUserCategory uc = (SafeUserCategory) node;
        try{
            Factory.getUserCategoryManager("").updateUserCategory(uc.getId(), uc.getName(), uc.getDescription());
        } catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean moveNode(String parentId, String id, int position) {
        Factory.getUserCategoryManager("").moveUserCategory(id, parentId);
        return true;
    }

    @Override
    public String getContextMenuPage() {
        return "policy/userCategory/contextMenu";
    }

    @Override
    public BaseModel getNodeById(String id) {
        return Factory.getUserCategoryManager("").getUserCategory(id);
    }

    @Override
    public String getModifyPage() {
        return "policy/userCategory/modify";
    }

    @Override
    public String getTreeType() {
        return "userCategory";
    }

    @Override
    public boolean copyNode(Object node) {
        SafeUserCategory uc = (SafeUserCategory) node;
        try{
            Factory.getUserCategoryManager("").copyUserCategory(uc.getId(), uc.getName(), uc.getDescription());
        } catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
